export function debounce(func, ms, leading = true) {
  let timerId;
  let lastCallTime;

  return function throttledFunc(...args) {
    const now = Date.now();
    
    if (leading && !lastCallTime) {
      func.apply(this, args);
      lastCallTime = now;
    } else {
      const remaining = ms - (now - lastCallTime);

      if (!timerId) {
        timerId = setTimeout(() => {
          timerId = null;
          lastCallTime = leading ? now : undefined;
          func.apply(this, args);
        }, remaining);
      }
    }
  };
}

